<?php

class Files extends CActiveRecord
{
	public $delete;
    public $oldFile;
    public $oldName;
    public $pasteOption;
    public $pasteAll;

	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function tableName()
	{
		return '{{files}}';
	}

	public function rules()
	{
		return array(
			array('file_dir_id, file_name, file_src', 'required'),
			array('file_dir_id', 'numerical', 'integerOnly'=>true),
			array('file_name, file_exp, oldFile, oldName, file_src', 'length', 'max'=>128),
			array('id, file_dir_id, file_name, file_exp', 'safe', 'on'=>'search'),
		);
	}

	public function relations()
	{
		return array(
			'fileDir' => array(self::BELONGS_TO, 'Directories', 'file_dir_id'),
		);
	}

	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'dir_id' => 'File Dir',
			'name' => 'File Name',
			'exp' => 'File Exp',
            'src' => 'File Src'
		);
	}

	public function search()
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id', $this->id);
        $criteria->compare('oldFile', $this->oldFile);
		$criteria->compare('dir_id', $this->file_dir_id);
		$criteria->compare('name', $this->file_name,true);
		$criteria->compare('exp', $this->file_exp,true);
        $criteria->compare('src', $this->file_src,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    public function afterSave()
    {
        $newFile=$this->path.DIRECTORY_SEPARATOR.$this->fileName;

        if(!is_file($newFile) && $this->oldFile && !copy($this->oldFile, $newFile)){
                $this->delete();
                return false;
        }elseif(!is_file($newFile) && $this->oldName && !rename($this->oldName, $newFile)){
            return false;
        }

        return true;
    }

    public function beforeDelete()
    {
        if($this->oldFile && unlink($this->oldFile)){
            return true;
        }
        return false;
    }

    public function getPath()
    {
        $dir=new Directories;
        return $dir->findByPk($this->file_dir_id)->path;
    }

    public function getName()
    {
        return $this->file_name;
    }

    public function getFileName()
    {
        return $this->file_name.'.'.$this->file_exp;
    }

    public function getExp()
    {
        return $this->file_exp;
    }

    public function getDirId()
    {
        return $this->file_dir_id;
    }

    public function getSrc()
    {
        return $this->file_src;
    }

    public function getDir()
    {
        $dir=new Directories;
        return $dir->findByPk($this->dirId);
    }

    public function getLocation()
    {
        return $this->path.DIRECTORY_SEPARATOR.$this->fileName;
    }

    public function getX()
    {
        return $this->getImgSize('x');
    }

    public function getY()
    {
        return $this->getImgSize('y');
    }

    public function setSrc($value)
    {
        $this->file_src=$value;
    }

    private function getImgSize($param=false)
    {
        list($width, $height)=getimagesize($this->location);
        if($param=='x'){
            return $width;
        }elseif($param=='y'){
            return $height;
        }elseif($param===false){
            return array($width, $height);
        }
        return false;
    }
}